package ebedynky.controllers;

import ebedynky.beans.*;
import ebedynky.editors.EditorFactory;
import ebedynky.services.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.*;
import org.springframework.validation.*;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ondrej David (davidon1)
 */
@Controller
public class OrderItemController {

    /**
     * Initialises the data binder.
     * @param binder the data binder to be initialised
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        try {
            //        binder.setValidator(new OrderItemValidator());
            binder.registerCustomEditor(OfferItem.class, EditorFactory.getOfferItemIDEditor());
            binder.registerCustomEditor(BoxOrder.class, EditorFactory.getBoxOrderIDEditor());
            binder.registerCustomEditor(OrderItem.class, EditorFactory.getOrderItemIDEditor());
        } catch (Exception ex) {
            Logger.getLogger(OrderItemController.class.getName()).log(Level.SEVERE, "Binder failed at OrderItemController", ex);
        }
    }

    /**
     * Fetches the OrderItem adding page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value = "/orderItemAdd", method = RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute(new OrderItem());
        return "order/item/createForm";
    }

    /**
     * Extracts the OrderItem data from the form,
     * validates them and stores them into the db.
     * @param orderItem data from the form
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value = "/orderItemAdd", method = RequestMethod.POST)
    public String create(@Valid OrderItem orderItem, BindingResult result) {
        if (result.hasErrors()) {
            return "order/item/createForm";
        }
        OrderItemService ois = new OrderItemService();
        ois.addOrderItem(orderItem);
        return "order/index";
    }

    /**
     * Fetches the OrderItem delete page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value = "/orderItemDel", method = RequestMethod.GET)
    public String getDeleteForm(Model model) {
        model.addAttribute(new OrderItem());
        return "order/item/deleteForm";
    }

    /**
     * Deletes the corresponding OrderItem data from the db.
     * @param orderItem data from the form
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value = "/orderItemDel", method = RequestMethod.POST)
    public String delete(@Valid OrderItem orderItem, BindingResult result) {
        if (result.hasErrors()) {
            return "order/item/deleteForm";
        }
        OrderItemService ois = new OrderItemService();
        ois.deleteOrderItem(orderItem.getOrderItemID());
        return "order/index";
    }

    /**
     * Adds a OrderItem map extracted from the db as an attribute to the model.
     * @return Map<Integer, String> map of OrderItemIDs and names
     */
    @ModelAttribute("orderItemList")
    protected Map<Integer, String> orderItemData() {
        Map<Integer, String> oiMap = new ConcurrentHashMap<Integer, String>();
        OrderItemService ois = new OrderItemService();
        List<OrderItem> list = ois.getAllOrderItems();
        Iterator<OrderItem> it = list.iterator();
        OrderItem oi;
        while (it.hasNext()) {
            oi = it.next();
            oiMap.put(oi.getOrderItemID(), oi.toString());
        }
        return oiMap;
    }

    /**
     * Adds a OfferItem map extracted from the db as an attribute to the model.
     * @return Map<Integer, String> map of OfferItemIDs and names
     */
    @ModelAttribute("offerItemList")
    protected Map<Integer, String> offerItemData() {
        Map<Integer, String> oiMap = new ConcurrentHashMap<Integer, String>();
        OfferItemService ois = new OfferItemService();
        List<OfferItem> list = ois.getAllOfferItems();
        Iterator<OfferItem> it = list.iterator();
        OfferItem oi;
        while (it.hasNext()) {
            oi = it.next();
            oiMap.put(oi.getOfferItemID(), oi.toString());
        }
        return oiMap;
    }

    /**
     * Adds a OrderItem map extracted from the db as an attribute to the model.
     * @return Map<Integer, String> map of OrderItemIDs and names
     */
    @ModelAttribute("boxOrderList")
    protected Map<Integer, String> orderData() {
        Map<Integer, String> boMap = new ConcurrentHashMap<Integer, String>();
        BoxOrderService bos = new BoxOrderService();
        List<BoxOrder> list = bos.getAllBoxOrders();
        Iterator<BoxOrder> it = list.iterator();
        BoxOrder bo;
        while (it.hasNext()) {
            bo = it.next();
            boMap.put(bo.getBoxOrderID(), bo.toString());
        }
        return boMap;
    }
}
